//
//  NSObject+Property.m
//  UnionDrugHealthy
//
//  Created by duanan on 2020/3/11.
//  Copyright © 2020 duanan. All rights reserved.
//

#import "NSObject+Property.h"
#import <objc/runtime.h>
 
@implementation NSObject (Property)


//获取类的所有属性名
- (NSArray*)getPropertieNamesByObject:(id)object
{
    
    unsigned int outCount, i;
    
    // 获取注册类的属性列表，第一个参数是类，第二个参数是接收类属性数目的变量
    objc_property_t *properties = class_copyPropertyList([object class], &outCount);
    //定义一个数组来接收获取的属性名
    NSMutableArray *nameArray = [[NSMutableArray alloc] initWithCapacity:outCount];
    for (i = 0; i < outCount; i++) {
        //通过循环来获取单个属性
        objc_property_t property = properties[i];
        //取得属性名
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        //将得到的属性名放入数组中
        [nameArray addObject:propertyName];
        
    }
    free(properties);
    return nameArray;
}

 
@end
